#!/bin/bash

###参数说明
#======================================= 传参 ======================================
# 网络框架环境: int; stg; prd
env=prd
# 原版本号
S_VERSION="1.0.0"
# 修改后的版本号
T_VERSION="1.0.0"

# 原程序显示名称
S_APP_DISPLAY_NAME="XXXX"
# 修改后成型显示名称
T_APP_DISPLAY_NAME="XXXX"
# 设备类型
device="iPhone"
# 打包描述文件名称
PROVISION_PROFILE_NAME="XXXX"
# 打包证书名称
CODESIGN_INDENTITY_NAME="XXXX"
# APP ID
BUNDLE_IDENTIFIER="XXXX"


#====================================== path ========================================
# 工作空间 （在不同机器上需要修改！！！）
workspace=`pwd`

#===================================== function =====================================
#获取包名后缀
function getPackageFix(){
if [ "$env" == "prd" ];then
echo "prd";
elif [ "$env" == "int" ];then
echo "int";
elif [ "$env" == "stg" ];then
echo "stg";
fi
}

#获取网络框架环境
function getNetwork(){
if [ "$env" == "prd" ];then
echo "2"
elif [ "$env" == "int" ];then
echo "0"
elif [ "$env" == "stg" ];then
echo "1"
fi
}

#获取时间戳，格式：yyyymmddHHMMSS
function getTime(){
echo `date +%Y``date +%m``date +%d``date +%H``date +%M``date +%S`;
}

#获取时间戳，格式：yyyy-mm-dd HH:MM:SS
function getTimeFormat(){
echo `date +%Y`-`date +%m`-`date +%d`_`date +%H`:`date +%M`:`date +%S`;
}

#获取时间，格式：mmddhhmm
function getTime_mmddhhmm(){
echo `date +%m``date +%d``date +%H``date +%M`
}

#获取设备类型简称
function getDeviceType(){
if [ "$device" == "iPhone" ];then
echo "h"
elif [ "$device" == "iPad" ];then
echo "a"
fi
}

#获取版本号
function getVersion(){
echo "v""$T_VERSION"
}

#======================================= common config ===================================
#工程路径

projectName="XXXX" #工程名字
projectTmpName="tmp"
projectBase=$workspace/$projectName
projectTmpBase=$workspace/$projectTmpName

#ipa包输出路径
outputName="output"
output=$workspace/$outputName

#编译参数
configuration="Release"
targetStr=$projectName;
iphoneos="iphoneos"
IPHONEOS_DEPLOYMENT_TARGET="8.0"
PRODUCTS="Products"

#ipa名称
ipaMain=$projectName;
ipaName=${ipaMain}-$(getPackageFix)-$(getVersion)-$(getTime).ipa
ipaShortName=$ipaMain.ipa
appName=$targetStr.app
DSYMName=$targetStr.app.dSYM

#===================================== 初始化 ======================================

cd $workspace
mkdir -pv $output
#静态代码扫描
#mkdir -pv $REPORT_DIRNAME
#mkdir -pv $REPORT_DIRNAME/$SCAN_BUILD_DIRNAME
cd $output
rm -rf $ipaShortName

#============================ 创建工程备份 ==========================================
cd $workspace
echo "remove project bak start..."
rm -rf $projectTmpName
echo "remove project bak end."

echo "make project bak start......"
cp -rf $projectName $projectTmpName
echo "make project bak end."

#======================================字符替换 start ===============================
#1.设置网络框架环境
#fn="MO_Global.h"
#cd $projectTmpBase/MobileOffice/公共/公共配置
#sed "s/#define PAMO_ENVIRONMENT.*\/\/网络框架环境/#define PAMO_ENVIRONMENT $(getNetwork) \/\/网络框架环境/g" $fn>1.h
#cat 1.h>$fn
#rm -rf 1.h

#echo "print $fn start..."
#cat $fn
#echo "print $fn end."

#2.修改Bundle identifier,app显示名,设置版本号
fn="Info.plist"
cd $projectTmpBase/$projectName
echo $projectTmpBase/$projectName
sed "s/<string>com.*<\/string>/<string>${BUNDLE_IDENTIFIER}<\/string>/g" $fn>1.plist
sed "s/<string>${S_APP_DISPLAY_NAME}<\/string>/<string>${T_APP_DISPLAY_NAME}<\/string>/g" 1.plist>2.plist
sed "s/<string>${S_VERSION}<\/string>/<string>${T_VERSION}<\/string>/g" 2.plist>3.plist
#sed "s/<string>${S_VERSION}<\/string>/<string>${T_VERSION}<\/string>/g" 3.plist>4.plist
cat 3.plist>$fn
#rm -rf 4.plist
rm -rf 3.plist
rm -rf 2.plist
rm -rf 1.plist
echo "print $fn start..."
cat $fn
echo "print $fn end."

#3、设置描述文件和签名
fn="project.pbxproj"
cd $projectTmpBase/$targetStr.xcodeproj
#替换描述文件
sed "s/PROVISIONING_PROFILE = \".*\"/PROVISIONING_PROFILE = \"${PROVISION_PROFILE_NAME}\"/g" $fn>1.pbxproj
sed "s/PROVISIONING_PROFILE_SPECIFIER = .*/PROVISIONING_PROFILE_SPECIFIER = \"${PROVISION_PROFILE_NAME}\";/g" 1.pbxproj>2.pbxproj
#替换证书名
sed "s/CODE_SIGN_IDENTITY = \".*\"/CODE_SIGN_IDENTITY = \"${CODESIGN_INDENTITY_NAME}\"/g" 2.pbxproj>3.pbxproj
sed "s/\"CODE_SIGN_IDENTITY\[sdk=iphoneos\*]\" = \".*\"/\"CODE_SIGN_IDENTITY\[sdk=iphoneos\*]\" = \"${CODESIGN_INDENTITY_NAME}\"/g" 3.pbxproj>4.pbxproj
#替换bundleId
sed "s/PRODUCT_BUNDLE_IDENTIFIER = .*/PRODUCT_BUNDLE_IDENTIFIER = ${BUNDLE_IDENTIFIER};/g" 4.pbxproj>5.pbxproj
cat 5.pbxproj>$fn
rm -rf 5.pbxproj
rm -rf 4.pbxproj
rm -rf 3.pbxproj
rm -rf 2.pbxproj
rm -rf 1.pbxproj
echo "print $fn start..."
cat $fn
echo "print $fn end."


#====================================== 编译与打包 ==========================================
#clean and compile
appPath="$projectTmpBase/build/$PRODUCTS/$configuration-iphoneos/$appName"
ipaPathWithResigned="$projectTmpBase/${ipaName}"
echo $ipaPathWithResigned
cd $projectTmpBase

echo "clean start..."
#unlock the keychain
#security unlock-keychain -p "password"
#$HOME/Library/Keychains/login.keychain
echo "clean end."
echo "compile start......"
echo $projectTmpBase/$targetStr.xcworkspace
echo $projectName
echo $CODESIGN_INDENTITY_NAME
echo $PROVISION_PROFILE_NAME


#定义了一个xcodebuild_workspace打包方法
function xcodebuild_workspace(){
echo "start workspace......"
xcodebuild \
-workspace $projectTmpBase/$targetStr.xcworkspace \
-scheme $projectName \
-configuration $configuration \
CODE_SIGN_IDENTITY="iPhone Distribution: Ping'an Securities Co.,Ltd." \
PROVISIONING_PROFILE=$PROVISION_PROFILE_NAME \
clean \
build \
-derivedDataPath $projectTmpBase
echo "compile end."
echo "xcrun start......"
xcrun -sdk iphoneos PackageApplication \
-v $appPath \
-o $ipaPathWithResigned
}


#定义了xcodebuilid_project 打包方法
function xcodebuilid_project(){
appPath="$projectTmpBase/build/$configuration-iphoneos/$appName"
xcodebuild clean -configuration $configuration -target $targetStr
echo "start project......"
xcodebuild -configuration $configuration -sdk $iphoneos -target $targetStr IPHONEOS_DEPLOYMENT_TARGET=$IPHONEOS_DEPLOYMENT_TARGET;
echo "compile end."
echo "xcrun start......"
xcrun -sdk iphoneos PackageApplication -v $appPath -o $ipaPathWithResigned
echo "xcrun end."
}

#如果存在xcworkspace 就是workspace方式打包
if [ -e "$targetStr.xcworkspace" ];
then
xcodebuild_workspace;
#使用传统的打包方式
else
xcodebuilid_project;
fi







#====================================== 归档 ===============================================

#output the ipa file to special target
echo "move ipaFile to special target start......"

cd $output
timeDir=$(getTimeFormat)
if [ ! -d "$timeDir" ];then
mkdir $timeDir
fi

#归档.app,ipa,dSYM文件
cd $projectTmpBase
cp -rf $ipaPathWithResigned $output/$timeDir/$ipaShortName
mv $ipaPathWithResigned $output/$timeDir
mv $projectTmpBase/build/$configuration-iphoneos/$appName $output/$timeDir
mv $projectTmpBase/build/$configuration-iphoneos/$DSYMName $output/$timeDir
echo "move ipaFile to special target done."

#删除.svn文件
cd $projectTmpBase
find . -type d -name ".svn" |xargs rm -rvf
#归档打包源码
cd ..
zip -r -T source.zip $projectTmpName
mv source.zip $output/$timeDir

#压缩所有归档文件
allReleaseZipName="Release_${T_VERSION}.zip"
cd $output
if [ -f "${allReleaseZipName}" ];then
rm -rf ${allReleaseZipName}
fi
zip -r -T ${allReleaseZipName} $timeDir
